New-Alias -Name "touch"   NewFile
# New-Alias -Name "ListDir" Get-ChildItem
function NewFile($value) { New-Item -ItemType File $value }

# Set-Alias -Name "Go32"    "CD32"
# # https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.utility/set-alias?view=powershell-7.3
# Function CD32 { Set-Location -Path C:\Windows\System32 }

# 快速跳转到库根目录
New-Alias -Name "cdw"     "GotoWorkDir"
function GotoWorkDir {
    # via. chatgpt
    function InPackagesSubdirectory {
        param (
            [string]$CurrentPath = (Get-Location)
        )
        # 使用正则表达式判断路径是否匹配 @/packages, @/apps, @/app
        return ($CurrentPath -match ".*[\\\/]packages[\\\/].*" -or 
            $CurrentPath -match ".*[\\\/]apps[\\\/].*" -or 
            $CurrentPath -match ".*[\\\/]app[\\\/].*") 
    }
    if (InPackagesSubdirectory) {
        # Write-Output "warn not in monorepo"
        $command = "Set-Location ..\..\"
        Invoke-Expression $command
    }
    else {
        Write-Output "Not in monorepo!"
    }
}
## npm-command
New-Alias -Name "nd"      "PackageRunDev"
function PackageRunDev {
    param(
        [string]$params
    )
    Invoke-Expression "nr dev$params"
}


New-Alias -Name "nb"      "PackageRunBuild"
function PackageRunDev {
    param(
        [string]$params
    )
    Invoke-Expression "nr build$params"
}


New-Alias -Name "nt"      "PackageRunTest"
function PackageRunTest {
    param(
        [string]$params
    )
    Invoke-Expression "nr test$params"
}


New-Alias -Name "ntu"     "PackageRunTestUpdate"
function PackageRunTestUpdate {
    Invoke-Expression "nr test.update"
}


New-Alias -Name "ntc"     "PackageRunTestCoverage"
function PackageRunTestCoverage {
    Invoke-Expression "nr coverage"
}


New-Alias -Name "nl"      "PackageRunLint"
function PackageRunLint {
    param(
        [string]$params
    )
    Invoke-Expression "nr lint$params"
}


New-Alias -Name "ns"      "PackageRunStart"
function PackageRunStart {
    param(
        [string]$params
    )
    Invoke-Expression "nr start$params"
}


# @deprecated: use z or turbo
New-Alias -Name "pnpmp"   "PackageWithPrefix"
function PackageWithPrefix {
    param(
        [string]$path,
        [string]$params
    )
    $command = "pnpm --prefix $path run $params"
    Write-Output ">>>$path run $params"
    Invoke-Expression $command
}
# TODO: set(redict) github repo to my fork ver.
# New-Alias -Name "repo-to-me"

# Fix @antfu/ni
# Remove-Alias -Name ni -Force

New-Alias -Name "gpl"     "GitPull"
function GitPull {
    param(
        [string]$param
    )
    Invoke-Expression "git pull $param";
}
New-Alias -Name "gf"      "GitFetch"
function GitFetch {
    param(
        [string]$param
    )
    Invoke-Expression "git fetch $param";
}
New-Alias -Name "grm"      "GitRemote"
# TODO: https://learn.microsoft.com/zh-cn/previous-versions/powershell/module/microsoft.powershell.core/about/about_parameter_sets?view=powershell-7.3
function GitRemote {
    Invoke-Expression "git remote -v";
}


# MARK: exp 
function Exp {
    param(
        [string]$param
    )
    if ($param -eq "" -or $param -eq ".") {
        Invoke-Expression  "explorer.exe .\"
    }
    else {
        Invoke-Expression  "explorer.exe $param\"
    }
}

# MARK: Skip1
function Skip1 {
    # for swc/cli build
    Select-Object -Skip 1
}


function RunAndEcho {
    # [CmdletBinding()]
    param(
        [string]$param
    )
    Write-Host "$> $param"
    Invoke-Expression "$param";
}

# MARK: DropType
function DropType {
    param(
        [string]$params
    )
    try {
        TestCommand swc
        Invoke-Expression "swc $params -C jsc.target=esnext -d ."
    }
    catch {
        Write-Host "Error: no swc"
    }
}
# MARK: node-ts
New-Alias -Name "node-ts" "NodeTS"
function NodeTS {
    param(
        [string]$param
    )
    $ver = Invoke-Command "node --version";
    Write-Output "node ver = '$ver'"
    if ($ver -match "^v22\.*") {
        Invoke-Expression "node --experimental-strip-types $param"
    }
    else {
        Write-Output "this node version don't support exec with typescript, use @swc-node/register"
        Invoke-Expression "node -r @swc-node/register $param"
    }
}

function Get-SmallFiles {
    Param($Size)
    Get-ChildItem $HOME | Where-Object {
        $_.Length -lt $Size -and !$_.PSIsContainer
    }
}
function proxy {
    $Env:http_proxy = "http://127.0.0.1:$ProxyPort"
    $Env:https_proxy = "https://127.0.0.1:$ProxyPort"
}
function unproxy {
    $Env:http_proxy = ""
    $Env:https_proxy = ""
}
  
